<?php
/**************************************************************************
 *
 *   Copyright 2010 American Public Media Group
 *
 *   This file is part of AIR2.
 *
 *   AIR2 is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   AIR2 is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with AIR2.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************/

require_once 'BatchItem.php';

/**
 * BatchSource
 *
 * A source as the contents of a batch
 *
 * @property Source $Source
 * @author rcavis
 * @package default
 */
class BatchSource extends BatchItem {

    /**
     * Set the table columns
     */
    public function setTableDefinition() {
        parent::setTableDefinition();
        $this->setAttribute(Doctrine_Core::ATTR_EXPORT, Doctrine_Core::EXPORT_NONE);
    }


    /**
     * Set table relations
     */
    public function setUp() {
        parent::setUp();
        $this->hasOne('Source', array(
                'local' => 'bitem_xid',
                'foreign' => 'src_id',
            ));
    }


    /**
     * Get the mapped-UUID column for this model
     *
     * @return string
     */
    public function get_uuid_col() {
        return 'Source:src_uuid';
    }


    /**
     * Add custom search query (from the get param 'q')
     *
     * @param AIR2_Query $q
     * @param string  $alias
     * @param string  $search
     * @param boolean $useOr
     */
    public static function add_search_str(&$q, $alias, $search, $useOr=null) {
        // find the "Source" alias in the query
        $from_parts = $q->getDqlPart('from');
        foreach ($from_parts as $string_part) {
            if ($match = strpos($string_part, "$alias.Source")) {
                $offset = strlen("$alias.Source") + 1; // remove space
                $src_alias = substr($string_part, $match + $offset);

                Source::add_search_str($q, $src_alias, $search, $useOr);
                break;
            }
        }
    }


}
